var orderVm;
var creatOrderHttp;
var token='';
var openId='';
var unionId='';
var action='';//1代表学校订单
$(function(){

    token = getParam('token');
    openId = getParam('openId');
    action = getParam('action');
    window.localStorage.setItem("token",token);
    window.localStorage.setItem("openId",openId);

    orderVm = new Vue({
        el:'#order_id',
        data:{
            actId:'1',//活动ID
            payType:'1',//支付类型 1云闪付
            openId:openId,//openId
            unionId:unionId,//unionId
            userName:'',//姓名
            userPhone:'',//手机号
            longitude:'',//经度
            latitude:'',//纬度
            schoolName:'',//学校名称
            grade:'',//年级
            classGrade:'',//班级
            isSchool:false,//是否是学校
            price:'28.00',//原价
            number:'1',//购买数量
            prePrice:'1.00'//优惠价
        },
        methods:{
            init:function(){
                if(action==1){
                    this.isSchool=true;
                }
            },
            onClosePage:function(){
                // window.open("about:blank", "_top").close();
                window.close();
            },
            deductPrice:function(){
                return _toFixed(Number(this.price) - Number(this.prePrice),2);
            }
        }
    });
    orderVm.init();
});

creatOrderHttp=function(){

}

function placeOrder(){
    if(orderVm.userName==undefined || orderVm.userName==null || orderVm.userName==''){
        vant.Toast.fail('请输入姓名');
    }
    else if(orderVm.userPhone==undefined || orderVm.userPhone==null || orderVm.userPhone==''){
        vant.Toast.fail('请输入手机号');
    }
    else if(!checkPhone(orderVm.userPhone)){
        vant.Toast.fail('请输入正确的手机号码');
    }
    else if(orderVm.isSchool && (orderVm.schoolName==undefined || orderVm.schoolName==null || orderVm.schoolName=='')){
        vant.Toast.fail('请输入学校名称');
    }
    else if(orderVm.isSchool && (orderVm.grade==undefined || orderVm.grade==null || orderVm.grade=='')){
        vant.Toast.fail('请输入年级');
    }
    else if(orderVm.isSchool && (orderVm.classGrade==undefined || orderVm.classGrade==null || orderVm.classGrade=='')){
        vant.Toast.fail('请输入班级');
    }
    else{
        if(orderVm.isSchool)
            orderVm.actId = "2";//1元捐水给班级
        showVanLoading('下单中...');
        //var jsonData = {"name":orderVm.userName,"mobile":orderVm.userPhone};
        //alert(openId + " : " + orderVm.openId);
        var param="name="+orderVm.userName+"&mobile="+orderVm.userPhone +"&actId=" + orderVm.actId + "&openId=" + openId + "&unionId=" + unionId + "&payType=" + orderVm.payType + "&number=" + orderVm.number + "&longitude="+orderVm.longitude + "&latitude=" + orderVm.latitude;
        if(orderVm.isSchool){
            param = param + "&school="+orderVm.schoolName + "&grade=" + orderVm.grade + "&className=" + orderVm.classGrade;
            //jsonData = {"name":orderVm.userName,"mobile":orderVm.userPhone,"school":orderVm.schoolName,"grade":orderVm.grade,"className":orderVm.classGrade};
        }

        _getHttpsData('/lyPay/placeOrderNow',param,
            function(res){
                closeVanLoading();
                if(res.status){
                    var orderId=res.data;
                    window.location='../../html/order/pay_page.html?orderId='+orderId+'&token='+ token+'&openId='+openId+'&phone='+orderVm.userPhone;
                }else{
                    vant.Toast.fail(res.msg);
                }
            },
            function(err){
                closeVanLoading();
                vant.Toast.fail("网络异常"+err.readyState+":"+err.statusText+"("+err.status+")");
            }
        );
    }
    
}